package ui;

import java.awt.Color;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JPanel;
import java.awt.event.ContainerAdapter;
import java.awt.event.ContainerEvent;

public class ItemPanel2ListPanel extends JPanel {

	int gridLayoutAt = 0;
	boolean init = false;
	
	/**
	 * Create the panel.
	 */
	public ItemPanel2ListPanel(String text, Color foreground, Color background) {
		addContainerListener(new ContainerAdapter() {
			@Override
			public void componentRemoved(ContainerEvent e) {
				gridLayoutAt--;
			}
		});
		//final ItemPanel2ListPanel thisPanel = this;
		
		GridBagLayout gridBagLayout = new GridBagLayout();
//		gridBagLayout.columnWidths = new int[]{0, 0};
//		gridBagLayout.rowHeights = new int[]{0, 0};
//		gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
//		gridBagLayout.rowWeights = new double[]{0.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		
//		ItemDivider itemDivider = new ItemDivider((String) null, (Color) null, (Color) null);
//		ItemDivider itemDivider = new ItemDivider(text, (Color) null, (Color) null);
		ItemDivider itemDivider = new ItemDivider(text, foreground, background);
		GridBagConstraints gbc_itemDivider = new GridBagConstraints();
		gbc_itemDivider.weighty = 1.0;
		gbc_itemDivider.weightx = 1.0;
		gbc_itemDivider.anchor = GridBagConstraints.NORTH;
		gbc_itemDivider.fill = GridBagConstraints.HORIZONTAL;
		gbc_itemDivider.gridx = 0;
		gbc_itemDivider.gridy = 0;
		add(itemDivider, gbc_itemDivider);
//		this.add
		init = true;
	}
	
//	@Override 
//	Component addComponent(Component c)
//	{
//		
//	}

	@Override
	public Component add(Component comp)
	{
		if(init)
		{
			GridBagConstraints gbc = new GridBagConstraints();
			gbc.gridx = 0;
			gbc.gridy = (gridLayoutAt++) + 1;
			System.out.println("Adding at " + gbc.gridy);
			gbc.weightx = 1;
			gbc.weighty = 1;
			gbc.fill = GridBagConstraints.HORIZONTAL;
			super.add(comp, gbc);
			return null;
		}
		else
		{
			return super.add(comp);
		}
	}

	@Override
	public void add(Component comp, Object o)
	{
		if(init)
		{
			GridBagConstraints gbc = new GridBagConstraints();
			gbc.gridx = 0;
			gbc.gridy = (gridLayoutAt++) + 1;
			System.out.println("Adding at " + gbc.gridy);
			gbc.weightx = 1;
			gbc.weighty = 1;
			gbc.fill = GridBagConstraints.HORIZONTAL;
			super.add(comp, gbc);
		}
		else
		{
			super.add(comp, o);
		}
	}

	public int getNumberOfItems() {
		return this.getComponentCount();
	}

}
